package com.seo.controller.proxy.keyWord;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.seo.pojo.ReturnBody;
import com.seo.service.keyword.proxy.sdk.ProxyKeywordService;
import com.seo.service.keyword.sdk.KeywordService;
import com.seo.service.user.sdk.UserService;
import com.seo.utils.ClientConst;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: KeyWordController
 * @author rtc
 * @version 2018.07.28 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "proxy/keyWord/")
public class ProxyKeyWordController {

	@Resource(name = "keywordService")
	private KeywordService keywordService;

	@Resource(name = "proxyKeywordService")
	private ProxyKeywordService proxyKeywordService;
	
	@Resource(name = "userService")
	private UserService userService;

	/**
	 * 获取关键词报价
	 * 
	 * @param keyWord
	 *            关键词
	 * @param type
	 *            用户类型
	 * @return Map<String,Object>
	 */
	@RequestMapping(value = "/offer")
	public ModelAndView listOffer(@RequestParam Map<String, Object> param) throws Exception {
		log.info("获取关键词报价开始---param={}", param);
		ModelAndView mv = new ModelAndView();
		List<Map<String, Object>> keyWordOffer = null;
		Integer	userId = Jurisdiction.getT("userId", Integer.class, ClientConst.SESSION_PROXY_USER);
		Integer	id = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_PROXY_USER);
		if (!param.isEmpty()) {
			Map<String, Object> userById = userService.getUserProxyById(id);
			if (userById != null) {
				String price1 = userById.get("price_1") + "";
				String price2 = userById.get("price_2") + "";
				if (!StringUtils.isEmpty(price1) && !"0.0000".equals(price1) && !StringUtils.isEmpty(price2)
						&& !"0.0000".equals(price2)) {
					param.put("price1", price1);
					param.put("price2", price2);
					keyWordOffer = keywordService.getKeyWordOffer1(param);
					mv.addObject("keyWord", param.get("keyWord"));
					mv.addObject("keyWordOffer", keyWordOffer);
					mv.setViewName("proxy/keyWord/keyWord_offer");
					return mv;
				}
			}
			param.put("type", "01");
			param.put("userId", userId);
			keyWordOffer = keywordService.getKeyWordOffer(param);
		}
		mv.addObject("keyWord", param.get("keyWord"));
		mv.addObject("keyWordOffer", keyWordOffer);
		mv.setViewName("proxy/keyWord/keyWord_offer");
		log.info("获取关键词报价结束");
		return mv;
	}

	/**
	 * 跳转关键词新增
	 * 
	 * @param param
	 *            param
	 * @return ModelAndView
	 * @author rtc
	 * @version 2018.07.28 12:20
	 */
	@RequestMapping(value = "/toKeyWordAdd")
	public ModelAndView toKeyWordAdd() throws Exception {
		log.info("【跳转关键词新增】---【开始】---");
		ModelAndView mv = new ModelAndView();
		List<Map<String, Object>> engine = keywordService.getEngineList();
		mv.addObject("engine", engine);
		mv.setViewName("proxy/keyWord/keyWord_proxy_add");
		log.info("【跳转关键词新增】---【结束】---");
		return mv;
	}

	/**
	 * 关键词新增
	 * 
	 * @param param
	 *            param
	 * @return String
	 * @author rtc
	 * @version 2018.07.28 12:20
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public ReturnBody add(@RequestParam Map<String, Object> param) throws Exception {
		log.info("【关键词新增】---【开始】---");
		ReturnBody rb = ReturnBody.init();
		Map<String, Object> user = Jurisdiction.getUser(ClientConst.SESSION_PROXY_USER);
		try {
			if (user == null || user.isEmpty()) {
				return rb.error("用户不存在");
			}
			if (param == null || param.isEmpty()) {
				return rb.error("参数不能为空");
			}
			if (!param.containsKey("uploadType")) {
				return rb.error("请选择上传方式");
			}
			String uploadType = (String) param.get("uploadType");
			Map<String, Object> userById = userService .getUserProxyById(Integer.parseInt(user.get("id") + ""));
			BigDecimal balance = new BigDecimal(userById.get("balance")+"");
			BigDecimal balances = new BigDecimal("500");
			if(balance.compareTo(balances) < 0) {
				rb.error("余额过低，请充值！");
				return rb;
			}
			String price1 = userById.get("price_1") + "";
			String price2 = userById.get("price_2") + "";
			param.put("user", user);
			if ((StringUtils.isEmpty(price1) || "0.0000".equals(price1)) && (StringUtils.isEmpty(price2)
					|| "0.0000".equals(price2))) {
				rb.run(proxyKeywordService.saveProxyKeyWord(param, uploadType));
			} else {
				param.put("price1", price1);
				param.put("price2", price2);
				rb.run(proxyKeywordService.saveProxyKeyWord1(param, uploadType));
			}
			log.info("【关键词新增】---【结束】---");
			return rb;
		} catch (Exception e) {
			log.error("添加关键词异常[{}]", e);
			return rb.error();
		}

	}

}
